<?php


namespace app\home\controller;


use app\home\model\Company;
use app\home\model\WechatDepartment;
use app\home\model\WechatDepartmentUser;
use app\home\model\WechatUser;
use think\Db;

class Structure extends Base
{
    /**
     *  组织架构主页
     */
    public function index () {
        //公司简介
        $detail = Company::get();
        $detail['image'] = get_cover($detail['front_cover']);
        $this->assign('detail',json_encode($detail));
        //组织架构
        $departmentModel = new WechatDepartment();
        //一级部门
        $department = $departmentModel->where('parentid',1)->order('`order` desc')->select();
        foreach ($department as $key => $value) {
            $department[$key]['count'] = $this->getUserNumber($value['id']);
        }
        $list = [
            'department' => $department,
            'user' => []
        ];
        $this->assign('list',json_encode($list));

        return $this->fetch();
    }

    /**
     * 二级部门
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index2() {
        $id = input('id'); //部门id
        $department = Db::table("zm_wechat_department")->where('parentid',$id)->order('`order` desc')->select();
        if($department) {
            foreach ($department as $key => $value) {
                $department[$key]['count'] = $this->getUserNumber($value['id']);
            }
        }
        $user = Db::table("zm_wechat_user")->where(['department' => $id])->field('userid,name,header,avatar,department,position,gender,mobile')->order('`order` desc')->select();
        foreach ($user as $key => $value) {
            $user[$key]['header'] = ($value['header']) ? $value['header'] : $value['avatar'];
        }
        $list = [
            'department' => $department,
            'user' => $user
        ];
        $this->assign('list',json_encode($list));
        //获取对应部门名称
        $depart = WechatDepartment::get($id);
        $this->assign('title',$depart['name']);
        return $this->fetch();
    }

    /**
     * 三级部门
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index3() {
        $id = input('id'); //部门id
        $department = Db::table("zm_wechat_department")->where('parentid',$id)->order('`order` desc')->select();
        foreach ($department as $key => $value) {
            $department[$key]['count'] = $this->getUserNumber($value['id']);
        }
        $user = Db::table("zm_wechat_user")->where(['department' => $id])->field('userid,name,header,avatar,department,position,gender,mobile')->order('`order` desc')->select();
        foreach ($user as $key => $value) {
            $user[$key]['header'] = ($value['header']) ? $value['header'] : $value['avatar'];
        }
        $list = [
            'department' => $department,
            'user' => $user
        ];
        $this->assign('list',json_encode($list));
        //获取对应部门名称
        $depart = WechatDepartment::get($id);
        $this->assign('title',$depart['name']);
        return $this->fetch();
    }

    /**
     *  组织架构列表
     */
    public function peoplelist () {
        $department = input('id');
        $user = WechatUser::where(['department' => $department])->field('userid,name,header,avatar,department,position,gender,mobile')->order('`order` desc')->select();
        foreach ($user as $key => $value) {
            $user[$key]['header'] = ($value['header']) ? $value['header'] : $value['avatar'];
        }
        $list = [
            'department' => [],
            'user' => $user
        ];
        $this->assign('list',json_encode($list));
        //获取对应部门名称
        $depart = WechatDepartment::get($department);
        $this->assign('title',$depart['name']);
        return $this->fetch();
    }

    /**
     * @param $departmentid 获取部门对应人数
     * @return int
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    private function getUserNumber($departmentid) {
        $departmentModel = new WechatDepartment();
        $userModel = new WechatUser();
        $count = 0;
        //一级科室用户
        $department1 = $departmentModel->where('id',$departmentid)->find();
        if($department1) {
            $user1 = $userModel->where('department',$department1['id'])->count();
            $count = $count + $user1;
            //是否存在二级部门
            $department2 = $departmentModel->where('parentid',$department1['id'])->select();
            if($department2) {
                foreach ($department2 as $k2 => $v2) {
                    $user2 = $userModel->where('department',$v2['id'])->count();
                    $count += $user2;
                    //是否存在三级部门
                    $department3 = $departmentModel->where('parentid',$v2['id'])->select();
                    if($department3) {
                        foreach ($department3 as $k3 => $v3) {
                            $user3 = $userModel->where('department',$v3['id'])->count();
                            $count += $user3;
                        }
                    }
                }
            }
        }
        return $count;
    }
}
